// pages/cart/cart.js
Page({

  doTakePhoto() {
    this.ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        console.log("success res=", res);
        this.setData({
          src: res.tempImagePath
        })
      }
    })
  },

  takePhoto() {
    // 可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.record" 这个 scope
    wx.getSetting({
      success: (res) => {
        console.log("getSetting", res);

        if (!res.authSetting['scope.camera']) {
          
          // 启动授权流程等待用户确认
          wx.authorize({
            scope: 'scope.camera',
            success: () => {
              this.doTakePhoto()
            },
            fail(err){
              console.log("auth err",err);
            }
          })
        } else {
          this.doTakePhoto()
        }
      }
    })



  },

  getStorage(e) {
    wx.getStorage({
      key: "userProfile"
    }).then(res => {
      console.log("get data ok:", res);
    }).catch(
      err => console.log("get data err", err)
    )
  },

  setStorage(e) {
    wx.setStorage({
      key: "userProfile",
      data: JSON.stringify({
        name: "蛋哥",
        age: 18
      })
    }).then(res => {
      console.log("save ok:", res);
    }).catch(err => {
      console.log("save err:", err);
    })
  },

  getUserProfile(e) {
    wx.getUserProfile({
      desc: '交出信息来！',
      success: (res) => {
        console.log("getUserProfile success", res);
      },
      fail(err) {
        console.log("getUserProfile fail:", err);
      }
    })
  },

  /**
   * 页面的初始数据
   */
  data: {
    src: "/img/1.jpg"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.ctx = wx.createCameraContext()
  },

})